package com.huali.Servlet;

import com.alibaba.fastjson.JSON;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@WebServlet(name = "BaseServlet", value = "/BaseServlet")
public class BaseServlet extends HttpServlet {
    private Object request;

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求编码格式
        request.setCharacterEncoding("UTF-8");

        //设置响应编码格式
        response.setContentType("text/html;charset=UTF-8");

        //获取方法参数
        String m = request.getParameter("m");
        //参数不为空时
        if (m != null && !"".equals(m)){
            try {
                Method method = this.getClass()
                        .getDeclaredMethod(m,
                                HttpServletRequest.class,
                                HttpServletResponse.class);
                Object value = method.invoke( this,request,response);

                response.getWriter().write(JSON.toJSONString(value));
            } catch (Exception e) {
                e.printStackTrace();
            }
            }else {
            response.getWriter().write(JSON.toJSONString("找不到方法"));
        }
        }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
